<template>
  <el-cascader size="mini" :options="treeData" :props="props" :value="value" separator="-" @change="changeValue" />
</template>
<script>
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
export default {
  props: {
    value: {
      type: Number, // 存储部门的ID
      default: null
    }
  },
  data() {
    return {
      treeData: [],
      props: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      this.treeData = transListToTreeData(await getDepartment(), 0) // 获取数据 转化树形 赋值data
    },
    changeValue(list) {
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1]) // 触发父组件的input事件
      } else {
        this.$emit('input', null) // 触发父组件的input事件
      }
    }
  }
}
</script>

